package com.java110.mall.acct.payment.business;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.account.AccountBondObjDto;
import com.java110.dto.payment.PaymentOrderDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.shopSms.ShopSmsDto;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.shop.IShopSmsV1InnerServiceSMO;
import com.java110.mall.acct.payment.IPaymentBusiness;
import com.java110.po.shopSms.ShopSmsPo;
import com.java110.po.shopSmsDetail.ShopSmsDetailPo;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.exception.CmdException;
import com.java110.utils.lock.DistributedLock;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service("shopBuySms")
public class ShopBuySmsBusiness implements IPaymentBusiness {

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    private IShopSmsV1InnerServiceSMO shopSmsV1InnerServiceSMOImpl;


    @Override
    public PaymentOrderDto unified(ICmdDataFlowContext context, JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "buyCount", "未包含购买条数");
        Assert.hasKeyAndValue(reqJson, "shopId", "未包含店铺");

        //计算服务费
        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(reqJson.getString("shopId"));
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);

        Assert.listOnlyOne(shopDtos, "商铺不存在");
        shopDto = shopDtos.get(0);

        String smsPrice = MappingCache.getValue("SMS_DOMAIN", "SHOP_SMS_PRICE");
        if (StringUtil.isEmpty(smsPrice)) {
            smsPrice = "0.1";
        }

        String orderId = GenerateCodeFactory.getGeneratorId("11");
        String feeName = shopDtos.get(0).getShopName() + "购买短信";
        BigDecimal amount = new BigDecimal(smsPrice);
        amount = amount.multiply(new BigDecimal(reqJson.getString("buyCount"))).setScale(2, BigDecimal.ROUND_HALF_UP);
        PaymentOrderDto paymentOrderDto = new PaymentOrderDto();
        paymentOrderDto.setOrderId(orderId);
        paymentOrderDto.setMoney(amount.doubleValue());
        paymentOrderDto.setName(feeName);
        paymentOrderDto.setProfitSharing(false);

        return paymentOrderDto;
    }

    @Override
    public void notifyPayment(PaymentOrderDto paymentOrderDto, JSONObject reqJson) {

        String orderId = paymentOrderDto.getOrderId();
        Assert.hasKeyAndValue(reqJson, "buyCount", "未包含购买条数");
        Assert.hasKeyAndValue(reqJson, "shopId", "未包含店铺");


        String requestId = DistributedLock.getLockUUID();
        String key = this.getClass().getSimpleName() + reqJson.getString("shopId");

        try {
            //开启事务
            DistributedLock.waitGetDistributedLock(key, requestId);
            ShopSmsDto shopSmsDto = new ShopSmsDto();
            shopSmsDto.setShopId(reqJson.getString("shopId"));
            List<ShopSmsDto> shopSmsDtos = shopSmsV1InnerServiceSMOImpl.queryShopSmss(shopSmsDto);
            if (ListUtil.isNull(shopSmsDtos)) {
                throw new IllegalArgumentException("未找到店铺短信");
            }

            ShopSmsPo shopSmsPo = new ShopSmsPo();
            int smsCount = Integer.parseInt(shopSmsDtos.get(0).getSmsCount());
            smsCount += reqJson.getIntValue("buyCount");
            shopSmsPo.setSmsCount(smsCount + "");
            shopSmsPo.setSmsId(shopSmsDto.getSmsId());
            shopSmsV1InnerServiceSMOImpl.updateShopSms(shopSmsPo);

        } finally {
            //清理事务信息
            DistributedLock.releaseDistributedLock(key, requestId);
        }

    }
}
